package site

import (
	"fmt"

	"github.com/playwright-community/playwright-go"

	"torler.com/qingyue/common/model"
	"torler.com/qingyue/common/types"
	"torler.com/qingyue/pkg/tools"
)

/**
 * 站点2解析器:81中文网
 */
type Site2Parser struct {
}

func (s *Site2Parser) GetSiteInfo() *SiteInfo {
	return newSiteInfo(2, "81中文网", "https://www.81zw2.com")
}

// 最新小说
func (s *Site2Parser) ParseLastUpdate(page playwright.Page) ([]*model.LastestCBook, error) {
	cbooks := []*model.LastestCBook{}
	items, err := page.Locator("div.l>ul>li").All()
	if err != nil {
		return nil, err
	}
	for _, item := range items {
		lastChapteUrl, _ := item.Locator("span.s3>a").GetAttribute("href")
		bookId := tools.FirstNumberInt(lastChapteUrl)
		book := model.NewLastUpdateCBook(s.GetSiteInfo().Id, types.OriginId(bookId))
		book.SetLastChapterIdWithUrl(lastChapteUrl)
		book.BookName, _ = item.Locator("span.s2>a").TextContent()
		book.Author, _ = item.Locator("span.s4").TextContent()
		cbooks = append(cbooks, book)
	}

	return cbooks, nil
}

func (s *Site2Parser) GetLastUpdateUrl() string {
	return s.GetSiteInfo().Host
}

func (s *Site2Parser) GetLastUpdateSelector() types.Selector {
	return `div.l>ul`
}

func (s *Site2Parser) ParseBookInfo(page playwright.Page) (*model.RawBook, error) {
	bookName, _ := page.Locator(`meta[property="og:novel:book_name"]`).GetAttribute("content")
	desc, _ := page.Locator(`div.intro>dl>dd`).TextContent()

	category, _ := page.Locator(`meta[property="og:novel:category"]`).GetAttribute("content")
	author, _ := page.Locator(`meta[property="og:novel:author"]`).GetAttribute("content")
	writeStatus, _ := page.Locator(`meta[property="og:novel:status"]`).GetAttribute("content")
	url, _ := page.Locator(`meta[property="og:url"]`).GetAttribute("content")
	obookId := types.NewOriginId(tools.LastNumber(url))
	lastUpateTime, _ := page.Locator(`meta[property="og:novel:update_time"]`).GetAttribute("content")
	imageUrl, _ := page.Locator(`meta[property="og:image"]`).GetAttribute("content")

	book := model.NewRawBook(s.GetSiteInfo().Id, obookId)
	book.BookName = bookName
	book.Desc = desc
	book.CategoryName = category
	book.Author = author
	book.WriteState = types.NewWriteState(writeStatus)
	book.LastUpdateTime.Time = tools.ParseTime(lastUpateTime)
	book.ImgUrl = imageUrl

	//章节列表处理
	items, err := page.Locator("dd>a[href^='/book']").All()
	if err != nil {
		return book, err
	}
	for _, item := range items {
		url, _ := item.GetAttribute("href")
		oid := types.NewOriginId(tools.LastNumber(url))
		name, _ := item.TextContent()
		chapter := model.NewRawChapter(oid, name)
		book.Chapters = append(book.Chapters, chapter)
	}

	return book, nil
}

func (s *Site2Parser) GetBookInfoUrl(originBookId types.OriginId) string {
	url := fmt.Sprintf("%s/book/%d/", s.GetSiteInfo().Host, originBookId)
	return url
}

// 小说章节
func (s *Site2Parser) ParseChapter(page playwright.Page) (string, error) {
	content, err := page.Locator("div[id='chaptercontent']").TextContent()
	if err != nil {
		return "", err
	}
	return content, nil
}

func (s *Site2Parser) GetChapterUrl(oBookId, oChapterId types.OriginId) string {
	url := fmt.Sprintf("%s/book/%d/%d.html", s.GetSiteInfo().Host, oBookId, oChapterId)
	return url
}

func (s *Site2Parser) GetChapterSelector() types.Selector {
	return "div[id='chaptercontent']"
}
